<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="售价" prop="selling">
      <el-input v-model="dataForm.selling" placeholder="售价"></el-input>
    </el-form-item>
    <el-form-item label="户型" prop="housetype">
      <el-input v-model="dataForm.housetype" placeholder="户型"></el-input>
    </el-form-item>
    <el-form-item label="单价/平方" prop="price">
      <el-input v-model="dataForm.price" placeholder="单价/平方"></el-input>
    </el-form-item>
    <el-form-item label="面积" prop="acreage">
      <el-input v-model="dataForm.acreage" placeholder="面积"></el-input>
    </el-form-item>
    <el-form-item label="楼层" prop="floor">
      <el-input v-model="dataForm.floor" placeholder="楼层"></el-input>
    </el-form-item>
    <el-form-item label="房屋类型" prop="housingTypes">
      <el-input v-model="dataForm.housingTypes" placeholder="房屋类型"></el-input>
    </el-form-item>
    <el-form-item label="更新" prop="housingUpdate">
      <el-input v-model="dataForm.housingUpdate" placeholder="更新"></el-input>
    </el-form-item>
    <el-form-item label="小区名称" prop="housingName">
      <el-input v-model="dataForm.housingName" placeholder="小区名称"></el-input>
    </el-form-item>
    <el-form-item label="地址" prop="location">
      <el-input v-model="dataForm.location" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="装修" prop="fitment">
      <el-input v-model="dataForm.fitment" placeholder="装修"></el-input>
    </el-form-item>
    <el-form-item label="朝向" prop="orientation">
      <el-input v-model="dataForm.orientation" placeholder="朝向"></el-input>
    </el-form-item>
    <el-form-item label="年代" prop="decade">
      <el-input v-model="dataForm.decade" placeholder="年代"></el-input>
    </el-form-item>
    <el-form-item label="租金" prop="rent">
      <el-input v-model="dataForm.rent" placeholder="租金"></el-input>
    </el-form-item>
    <el-form-item label="出租方式" prop="rentWay">
      <el-input v-model="dataForm.rentWay" placeholder="出租方式"></el-input>
    </el-form-item>
    <el-form-item label="支付方式" prop="payment">
      <el-input v-model="dataForm.payment" placeholder="支付方式"></el-input>
    </el-form-item>
    <el-form-item label="物业费" prop="property">
      <el-input v-model="dataForm.property" placeholder="物业费"></el-input>
    </el-form-item>
    <el-form-item label="楼盘Id外键" prop="housesId">
      <el-input v-model="dataForm.housesId" placeholder="楼盘Id外键"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateDay">
      <el-input v-model="dataForm.updateDay" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="特色" prop="feature">
      <el-input v-model="dataForm.feature" placeholder="特色"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          selling: '',
          housetype: '',
          price: '',
          acreage: '',
          floor: '',
          housingTypes: '',
          housingUpdate: '',
          housingName: '',
          location: '',
          fitment: '',
          orientation: '',
          decade: '',
          rent: '',
          rentWay: '',
          payment: '',
          property: '',
          housesId: '',
          updateDay: '',
          feature: ''
        },
        dataRule: {
          selling: [
            { required: true, message: '售价不能为空', trigger: 'blur' }
          ],
          housetype: [
            { required: true, message: '户型不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '单价/平方不能为空', trigger: 'blur' }
          ],
          acreage: [
            { required: true, message: '面积不能为空', trigger: 'blur' }
          ],
          floor: [
            { required: true, message: '楼层不能为空', trigger: 'blur' }
          ],
          housingTypes: [
            { required: true, message: '房屋类型不能为空', trigger: 'blur' }
          ],
          housingUpdate: [
            { required: true, message: '更新不能为空', trigger: 'blur' }
          ],
          housingName: [
            { required: true, message: '小区名称不能为空', trigger: 'blur' }
          ],
          location: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          fitment: [
            { required: true, message: '装修不能为空', trigger: 'blur' }
          ],
          orientation: [
            { required: true, message: '朝向不能为空', trigger: 'blur' }
          ],
          decade: [
            { required: true, message: '年代不能为空', trigger: 'blur' }
          ],
          rent: [
            { required: true, message: '租金不能为空', trigger: 'blur' }
          ],
          rentWay: [
            { required: true, message: '出租方式不能为空', trigger: 'blur' }
          ],
          payment: [
            { required: true, message: '支付方式不能为空', trigger: 'blur' }
          ],
          property: [
            { required: true, message: '物业费不能为空', trigger: 'blur' }
          ],
          housesId: [
            { required: true, message: '楼盘Id外键不能为空', trigger: 'blur' }
          ],
          updateDay: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          feature: [
            { required: true, message: '特色不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/floor/leadinghousing/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.selling = data.leadingHousing.selling
                this.dataForm.housetype = data.leadingHousing.housetype
                this.dataForm.price = data.leadingHousing.price
                this.dataForm.acreage = data.leadingHousing.acreage
                this.dataForm.floor = data.leadingHousing.floor
                this.dataForm.housingTypes = data.leadingHousing.housingTypes
                this.dataForm.housingUpdate = data.leadingHousing.housingUpdate
                this.dataForm.housingName = data.leadingHousing.housingName
                this.dataForm.location = data.leadingHousing.location
                this.dataForm.fitment = data.leadingHousing.fitment
                this.dataForm.orientation = data.leadingHousing.orientation
                this.dataForm.decade = data.leadingHousing.decade
                this.dataForm.rent = data.leadingHousing.rent
                this.dataForm.rentWay = data.leadingHousing.rentWay
                this.dataForm.payment = data.leadingHousing.payment
                this.dataForm.property = data.leadingHousing.property
                this.dataForm.housesId = data.leadingHousing.housesId
                this.dataForm.updateDay = data.leadingHousing.updateDay
                this.dataForm.feature = data.leadingHousing.feature
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/floor/leadinghousing/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'selling': this.dataForm.selling,
                'housetype': this.dataForm.housetype,
                'price': this.dataForm.price,
                'acreage': this.dataForm.acreage,
                'floor': this.dataForm.floor,
                'housingTypes': this.dataForm.housingTypes,
                'housingUpdate': this.dataForm.housingUpdate,
                'housingName': this.dataForm.housingName,
                'location': this.dataForm.location,
                'fitment': this.dataForm.fitment,
                'orientation': this.dataForm.orientation,
                'decade': this.dataForm.decade,
                'rent': this.dataForm.rent,
                'rentWay': this.dataForm.rentWay,
                'payment': this.dataForm.payment,
                'property': this.dataForm.property,
                'housesId': this.dataForm.housesId,
                'updateDay': this.dataForm.updateDay,
                'feature': this.dataForm.feature
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
